package _04Dom4j.XPath;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;


import java.util.List;

public class Demo {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("day14\\src\\_04Dom4j\\XPath\\xpath_contact.xml");
        Element rootElement = document.getRootElement();

        System.out.println("=======================================");
        //相对根节点，查找所有contact下的name节点
        List<Node> nodes = rootElement.selectNodes("./contact/name");
        for (Node node : nodes) {
            System.out.println("node.getText() = " + node.getText());
        }
        System.out.println("=======================================");
        //找到武大郎节点
        Node nodes1 = rootElement.selectSingleNode("./contact/brother/name");
        System.out.println(nodes1.getText());

        System.out.println("=======================================");
        //全文搜索name节点
        List<Node> nodeList = rootElement.selectNodes("//name");
        for (Node node : nodeList) {

            System.out.println(node.getText());
        }
        System.out.println("=======================================");

        List<Node> nodeList2 = rootElement.selectNodes("//contact");
        for (Node node : nodeList2) {
            Element element = (Element) node;
            System.out.println("element.attributeValue(\"id\") = " + element.attributeValue("id"));
        }
    }
}
